package com.easidea.tim.flex.remote.commodity
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;
	
	public class ProductDao extends BaseDao
	{
		private var _productService:HibernateRemoteObject = new HibernateRemoteObject();
		private static var _instance:ProductDao;
		
		public static function getInstance():ProductDao{
			if(_instance == null){
				_instance = new ProductDao();
			}
			return _instance;
		}
		
		
		public function ProductDao()
		{
			_productService.channelSet = this.buildChannelSet();
			_productService.destination="productService";	
			_productService.concurrency = "last";
			_productService.makeObjectsBindable = true;
			_productService.showBusyCursor = true;
		}
		
		/**
		 * 过滤产品名称
		 * @author: Harrison
		 */
		public function filterProductByName(name:String):void{
			_productService.addEventListener(FaultEvent.FAULT,_Fault);
			_productService.addEventListener(ResultEvent.RESULT,filterProductNameSuccess);
			_productService.filteProductByName(name);
		}
		
		private function filterProductNameSuccess(e:ResultEvent):void{
			_productService.removeEventListener(ResultEvent.RESULT,filterProductNameSuccess);
			if(e.result != null){
				Env.dispatchEvent(new CustomerEvent("filterProductNameSuccess",e.result));
			}
		}
		
		public function searchProduct(name:String, colorCode:String):void{
			_productService.addEventListener(FaultEvent.FAULT,_Fault);
			_productService.addEventListener(ResultEvent.RESULT,searchProdutSuccess);
			_productService.searchProductByNameAndColor(name,colorCode);
		}
		
		private function searchProdutSuccess(e:ResultEvent):void{
			_productService.removeEventListener(ResultEvent.RESULT,searchProdutSuccess);
			Env.dispatchEvent(new CustomerEvent("searchProductSuccess",e.result));
		}
		
		public function savePrice(id:int, price:Number):void{
			_productService.addEventListener(FaultEvent.FAULT,_Fault);
			_productService.addEventListener(ResultEvent.RESULT,savePriceResult);
			_productService.savePrice(id,price);
		}
		
		private function savePriceResult(e:ResultEvent):void{
			_productService.removeEventListener(ResultEvent.RESULT,savePriceResult);
			Env.dispatchEvent(new CustomerEvent("savePriceSuccess",e.result));
		}
		
		public function bulkSavePriceByName(productName:String, price:Number):void{
			_productService.addEventListener(FaultEvent.FAULT,_Fault);
			_productService.addEventListener(ResultEvent.RESULT,bulkSavePriceByNameResult);
			_productService.bulkSavePriceByName(productName,price);
		}
		
		private function bulkSavePriceByNameResult(e:ResultEvent):void{
			_productService.removeEventListener(ResultEvent.RESULT,bulkSavePriceByNameResult);
			Env.dispatchEvent(new CustomerEvent("bulkSavePriceByNameSuccess",e.result));
		}
		
		public function saveProduct(pName:String,colorCode:String,price:Number, user:String):void{
			_productService.addEventListener(FaultEvent.FAULT,_Fault);
			_productService.addEventListener(ResultEvent.RESULT,saveProductResult);
			_productService.saveProduct(pName,colorCode,price,user);
		}
		
		private function saveProductResult(e:ResultEvent):void{
			_productService.removeEventListener(ResultEvent.RESULT,saveProductResult);
			Env.dispatchEvent(new CustomerEvent("saveProductSuccess",e.result));
		}
		
		public function getPricedProductList(pName:String,colorCode:String):void{
			_productService.addEventListener(FaultEvent.FAULT,_Fault);
			_productService.addEventListener(ResultEvent.RESULT, getPricedProductListProcess);
			_productService.getPricedProductList(pName, colorCode);
		}
		
		private function getPricedProductListProcess(e:ResultEvent):void{
			_productService.removeEventListener(ResultEvent.RESULT,getPricedProductListProcess);
			var rtnList:ArrayCollection = new  ArrayCollection();
			if(e.result != null){
				for each (var objArray:Array in e.result ){
					var value:Object = {id:objArray[0], name:objArray[1],basicPrice:objArray[2]};
					rtnList.addItem(value);
				}
			}
			Env.dispatchEvent(new CustomerEvent("getPricedProductListSuccess", rtnList));
		}
	}
}